﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using ModelLayer;

namespace CinemaDbLib
{
	public class RoomDB
	{
		private Connection connection;

		public RoomDB()
		{
			connection = new Connection();
		}

		public List<Room> getRooms()
		{
			return where("");
		}

		public Room getRoomById(int id)
		{
			return singleWhere("id = " + id);
		}

		private Room singleWhere(String wClause)
		{
			Room room = null;

			string query = "SELECT * FROM Rooms WHERE " + wClause;
			connection.Open();
			SqlDataReader reader = connection.Read(query);

			if (reader.Read())
			{
				room = new Room();
				room.id = reader.GetInt32(0);
				room.name = reader.GetString(1);
			}

			reader.Close();
			connection.Close();

			return room;
		}

		private List<Room> where(String wClause)
		{
			List<Room> rooms = new List<Room>();

			string query;
			if (!wClause.Equals(""))
				query = "SELECT * FROM Rooms WHERE " + wClause;
			else
				query = "SELECT * FROM Rooms";
			connection.Open();
			SqlDataReader reader = connection.Read(query);

			while (reader.Read())
			{
				Room room = new Room();
				room.id = reader.GetInt32(0);
				room.name = reader.GetString(1);
				rooms.Add(room);
			}

			reader.Close();
			connection.Close();

			return rooms;
		}
	}
}
